package com.qunar.toku.handler;

import java.util.concurrent.ConcurrentHashMap;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;

/**
 * 抽象父类
 * 
 * @author zhangwan
 * @email wan.zhang@qunar.com
 */
public abstract class ChannelClientHandler extends SimpleChannelUpstreamHandler {
	private final InternalLogger logger = 
			InternalLoggerFactory.getInstance(ChannelClientHandler.class);
	
	//使用一个并发map存每个调用传入的匿名对象(作为回调函数)
	public final ConcurrentHashMap<String, ResultHandler> invokeMap = new ConcurrentHashMap<String, ResultHandler>();

	protected abstract void execute(Channel channel, Object message);

	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
		execute(ctx.getChannel(), e.getMessage());
	}
	
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
		logger.error("exception:" + e.getCause());
		ctx.getChannel().close();
	}

}
